package com.wjk.kylin.mall.product.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjk.kylin.id.core.uid.UidGenerator;
import com.wjk.kylin.lock.annotation.KylinLock;
import com.wjk.kylin.mall.common.core.dto.base.BaseOutDTO;
import com.wjk.kylin.mall.common.core.dto.base.DataIdInDTO;
import com.wjk.kylin.mall.common.core.dto.base.DataOutDTO;
import com.wjk.kylin.mall.common.web.transactional.TransactionalHandle;
import com.wjk.kylin.mall.product.beanmapper.ProductEntityMapper;
import com.wjk.kylin.mall.product.bo.ProductBO;
import com.wjk.kylin.mall.product.dto.ProductInDTO;
import com.wjk.kylin.mall.product.entity.ProductEntity;
import com.wjk.kylin.mall.product.mapper.ProductMapper;
import com.wjk.kylin.mall.product.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author: wangjinkui
 * @Date: 2021/5/28
 * @Description : 商品
 */
@Slf4j
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductEntity> implements IProductService {
    @Autowired
    private TransactionalHandle transactionalHandle;

    @Autowired
    @Qualifier("cachedUidGenerator")
    private UidGenerator uidGenerator;

    /**
     * 根据id查询数据
     *
     * @param inDTO
     * @return
     */
    @Override
    public BaseOutDTO getById(DataIdInDTO inDTO) {
        DataOutDTO<ProductBO> outDTO = new DataOutDTO<>();
        outDTO.setData(ProductEntityMapper.INSTANCE.entity2BO(baseMapper.selectById(inDTO.getId())));
        return outDTO;
    }

    @Override
    @KylinLock(keys = "#inDTO.productCode")
    public BaseOutDTO add(ProductInDTO inDTO) {
        //查询code是否存在
        LambdaQueryWrapper<ProductEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductEntity::getProductCode, inDTO.getProductCode());
        long count = super.count(wrapper);
        if (count > 0) {
            return BaseOutDTO.error("编码重复，code:" + inDTO.getProductCode());
        }

        ProductEntity entity = ProductEntityMapper.INSTANCE.dto2Entity(inDTO);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setId(uidGenerator.getUID());
        //执行事务
        boolean flag = transactionalHandle.execute(() -> addEntity(entity));
        log.info("ProductServiceImpl-add,flag:{}", flag);
        if (flag) {
            return BaseOutDTO.success();
        }
        return BaseOutDTO.error("操作失败，请重试！");
    }

    @Override
    public List<Long> getProductIdListByIDList(List<Long> productIdList) {
        if (CollectionUtil.isEmpty(productIdList)) {
            return new ArrayList<>();
        }
//        QueryWrapper<ProductEntity> wrapper = new QueryWrapper<>();
//        wrapper.select("id");
//        wrapper.in("id",productIdList);
//        List<Long> idList = super.listObjs(wrapper, s -> Long.parseLong(s.toString()));

//        LambdaQueryWrapper<ProductEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.select(ProductEntity::getId);
//        lambdaQueryWrapper.in(ProductEntity::getId, productIdList);
//        List<Long> idList = super.listObjs(lambdaQueryWrapper, s -> Long.parseLong(s.toString()));

        return baseMapper.getProductIdListByIDList(productIdList);
    }

    /**
     * 执行事务的方法
     */
    public boolean addEntity(ProductEntity entity) {
        return super.save(entity);
    }


}
